import { useRequest, useAntdTable } from '@/hooks';
import request from '@/utils/request';
import cmdbres from '@/data/cmdbres';

/**
 * 省分选项
 */
export function useProvinceOptions() {
    return useRequest(async () =>
        (
            await request({
                ...cmdbres.queryDistrictTreeInfo,
                data: {
                    baseInfo: {},
                    properties: {},
                },
            })
        ).retObj.map(({ provinceName, provinceCode }) => ({
            label: provinceName,
            value: provinceCode,
        }))
    );
}

/**
 * 验收列表
 */
export function useAcceptanceList(options) {
    return useAntdTable(async (tableData, formData) => {
        const { current, pageSize } = tableData;
        const { name, provinceCode, acceptanceStatus, createTime } = formData;

        return (
            await request({
                ...cmdbres.queryOssAcceptanceList,
                params: {
                    pageNum: current,
                    pageSize,
                    name: name || undefined,
                    provinceCode,
                    acceptanceStatus,
                    ...(createTime
                        ? {
                              startTime: createTime[0].clone().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
                              endTime: createTime?.[0].clone().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
                          }
                        : undefined),
                },
            })
        ).data;
    }, options);
}

/**
 * 删除验收
 */
export function useDeleteAcceptance(options) {
    return useRequest(
        (acceptanceMaterialId) =>
            request({
                ...cmdbres.deleteOssAcceptance,
                params: { acceptanceMaterialId },
            }),
        {
            manual: true,
            ...options,
        }
    );
}

/**
 * 操作日志列表
 */
export const useOperationLogList = (otherFormData = {}, options) => {
    return useAntdTable(async (tableData, formData = {}) => {
        const { current, pageSize } = tableData;
        const { resourceName, operator, operatorCode } = formData;

        return (
            await request({
                ...cmdbres.queryOssAcceptanceOperationLogList,
                data: {
                    page: current,
                    pageSize,
                    platformCode: 'omp',
                    moduleCode: 'CMDB',
                    resourceType: 'AcceptanceResourceManagement',
                    resourceName: resourceName || undefined,
                    operator: operator || undefined,
                    operatorCode,
                    ...otherFormData,
                },
            })
        ).data;
    }, options);
};
